<?php

/**
 * Yii Controller OptionController
 *
 * @author luubk
 * @since Jan 9, 2013 - 6:46:56 PM
 */
class OptionController extends BackController
{

    public $modelName = 'Option';

    public function init()
    {
        parent::init();

        $this->controllerMenu = array();

        foreach (Option::getArrayCategory() as $groupKey => $groupName)
        {
            $this->setingMenu[] = array(
                'label' => Yii::t('option', $groupName),
                'url' => $this->createUrl('option/index', array(
                    'lang' => $this->lang,
                    'category' => $groupKey,
                ))
            );
        }
        $this->controllerMenu[] = array(
            'label' => 'Cài đặt chung',
            'url' => $this->createUrl('option/index', array(
                'lang' => $this->lang,
            ))
        );
        $this->controllerMenu[] = array(
            'label' => 'Slider Trang chủ',
            'url' => $this->createUrl('slider/index', array('lang' => $this->lang)),
        );
        $this->controllerMenu[] = array(
            'label' => Yii::t('option', 'Menu setting'),
            'url' => $this->createUrl('Menu/index', array('lang' => $this->lang))
        );
        $this->controllerMenu[] = array(
            'label' => Yii::t('option', 'List'),
            'url' => $this->createUrl('option/list', array('lang' => $this->lang))
        );

        $this->controllerMenu[] = array(
            'label' => Yii::t('option', 'Create'),
            'url' => $this->createUrl('option/create', array('lang' => $this->lang))
        );

        $this->controllerMenu[] = array(
            'label' => Yii::t('option', 'Media'),
            'url' => $this->createUrl('option/media', array('lang' => $this->lang))
        );
    }

    public function actionIndex()
    {
        $this->checkAccess();

        if (isset($_POST['Option']))
        {
            foreach ($_POST['Option'] as $id => $value)
            {
                $option = Option::model()->findByPk($id);
                if ($option != NULL)
                {
                    $option->value = $value;
                    $option->update(array('value'));
                }

                if ($option->name == 'robots')
                {
                    $file = "robots.txt";
                    if (!file_exists($file))
                    {
                        $fh = fopen($file, 'w');
                        fwrite($fh, $value);
                        fclose($fh);
                    }
                }
            }

            $noti = Yii::t('form', '{action} successful!', array(
                    '{action}' => Yii::t('form', 'Update')
            ));
            Yii::app()->user->setFlash('noti', $noti);
        }

        // Nếu không chọn category nào khi vào thì sẽ lấy category đầu tiên
        if (!isset($_GET['category']) or $_GET['category'] == '')
            $_GET['category'] = current(array_keys(Option::getArrayCategory()));

        foreach (Option::getArrayCategory() as $cateKey => $cateName)
        {
            if ($cateKey == @$_GET['category'])
                $this->pageTitle = Yii::t('option', 'Setting') . " $cateName";
        }

        $criteria = new CDbCriteria();
        $criteria->compare("category", $_GET['category']);
        $criteria->compare("language_id", $this->lang_id);
        $criteria->order = "rank ASC";
        $options = Option::model()->findAll($criteria);

        $this->render('index', array('options' => $options));
    }

    public function actionList()
    {
        $this->checkAccess();
        $this->render('list');
    }

    public function actionMedia()
    {
        $this->checkAccess();
        $this->render('media');
    }

    function actionStatistic()
    {
        $this->redirect($this->createUrl('statistic/index', array(
                'lang' => $this->lang
        )));
    }

}

/** 
 * End file OptionController
 */
